﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using System.Web.Script.Services;

namespace JeasyuiCRUD
{
    public partial class Default : System.Web.UI.Page
    {
        public static EasyuiDemoEntities objEasyuiDemo = new EasyuiDemoEntities();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        [WebMethod]
        public static int SaveCustomerInfo(string name, string address, string city, string phoneno)
        {
            int retVal = 0;
            try
            {
                CustomerInfo cust = new CustomerInfo();
                cust.Name = name;
                cust.Address = address;
                cust.City = city;
                objEasyuiDemo.CustomerInfoes.Add(cust);
                objEasyuiDemo.SaveChanges();
                retVal = 1;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return retVal;
        }

        [WebMethod]
        public static int UpdateCustomerInfo(int ID, string name, string address, string city, string phoneno)
        {
            int retVal = 0;
            try
            {
                if (ID > 0)
                {
                    var custInfo = objEasyuiDemo.CustomerInfoes.Where(c => c.ID == ID).SingleOrDefault();
                    if (custInfo != null)
                    {
                        custInfo.Name = name;
                        custInfo.Address = address;
                        custInfo.City = city;
                        objEasyuiDemo.SaveChanges();
                        retVal = 1;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return retVal;
        }

        [WebMethod]
        public static int DeleteCustomerInfo(int ID)
        {
            int retVal = 0;
            try
            {
                if (ID > 0)
                {
                    var custInfo = objEasyuiDemo.CustomerInfoes.Where(c => c.ID == ID).SingleOrDefault();
                    if (custInfo != null)
                    {
                        objEasyuiDemo.CustomerInfoes.Remove(custInfo);
                        objEasyuiDemo.SaveChanges();
                        retVal = 1;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return retVal;
        }

        [WebMethod]
        public void BindGrid()
        {
            try
            {
                var custInfo = objEasyuiDemo.CustomerInfoes.ToList();
                string strJson = JsonConvert.SerializeObject(custInfo);
                //return strJson;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}